package com.zx.onlineeducation.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.onlineeducation.common.servicebase.exception.OnlineEducationException;
import com.zx.onlineeducation.service.edu.entity.ChapterDO;
import com.zx.onlineeducation.service.edu.entity.VideoDO;
import com.zx.onlineeducation.service.edu.entity.vo.ChapterTreeVO;
import com.zx.onlineeducation.service.edu.mapper.ChapterMapper;
import com.zx.onlineeducation.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.onlineeducation.service.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 章节 服务实现类
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-13
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, ChapterDO> implements ChapterService {

    /**
     * 课程章节Mapper接口
     */
    @Autowired
    private ChapterMapper chapterMapper;

    /**
     * 课程视频服务接口
     */
    @Autowired
    private VideoService videoService;

    /**
     * 获取课程章节树形结构数据列表
     *
     * @param courseId 课程ID
     * @return ChapterTreeVO对象的List集合 {@link ChapterTreeVO}
     * @author 郑雪
     * @since 2022-01-14
     */
    @Override
    public List<ChapterTreeVO> getTree(String courseId) {
        return chapterMapper.selectTree(courseId);
    }

    /**
     * 删除课程章节
     *
     * @param id 章节ID
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-15
     */
    @Override
    public boolean removeChapterById(String id) throws OnlineEducationException{
        // 1.根据章节ID查询视频个数
        QueryWrapper<VideoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", id);
        long count = videoService.count(queryWrapper);

        // 2.判断
        if (count > 0 ) {
            throw new OnlineEducationException(20001, "不能删除，该章节里有视频");
        }
        // 3.执行删除,返回结果
        return this.removeChapterById(id);
    }
}
